{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Convolution "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2D convolution "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def convolve(signal, kernel):\n",
    "    output = []\n",
    "    kernel_size = len(kernel)\n",
    "    padding = kernel_size // 2 # assume zero padding\n",
    "    padded_signal = [0] * padding + signal + [0] * padding\n",
    "    \n",
    "    for i in range(padding, len(signal) + padding):\n",
    "        sum = 0\n",
    "        for j in range(kernel_size):\n",
    "            sum += kernel[j] * padded_signal[i - padding + j]\n",
    "        output.append(sum)\n",
    "    \n",
    "    return output\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-2, -2, -2, -2, -2, 5]\n"
     ]
    }
   ],
   "source": [
    "signal = [1, 2, 3, 4, 5, 6]\n",
    "kernel = [1, 0, -1]\n",
    "output = convolve(signal, kernel)\n",
    "print(output)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3D convolution "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "def convolution(image, kernel):\n",
    "    # get the size of the input image and kernel\n",
    "    (image_height, image_width, image_channels) = image.shape\n",
    "    (kernel_height, kernel_width, kernel_channels) = kernel.shape\n",
    "    \n",
    "    # calculate the padding needed for 'same' convolution\n",
    "    pad_h = (kernel_height - 1) // 2\n",
    "    pad_w = (kernel_width - 1) // 2\n",
    "    \n",
    "    # pad the input image with zeros\n",
    "    padded_image = np.pad(image, ((pad_h, pad_h), (pad_w, pad_w), (0, 0)), 'constant')\n",
    "    \n",
    "    # create an empty output tensor\n",
    "    output_height = image_height\n",
    "    output_width = image_width\n",
    "    output_channels = kernel_channels\n",
    "    output = np.zeros((output_height, output_width, output_channels))\n",
    "    \n",
    "    # perform the convolution operation\n",
    "    for i in range(output_height):\n",
    "        for j in range(output_width):\n",
    "            for k in range(output_channels):\n",
    "                output[i, j, k] = np.sum(kernel[:, :, k] * padded_image[i:i+kernel_height, j:j+kernel_width, :])\n",
    "    \n",
    "    return output\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Input image:\n",
      "[[[ 1  2]\n",
      "  [ 3  4]]\n",
      "\n",
      " [[ 5  6]\n",
      "  [ 7  8]]\n",
      "\n",
      " [[ 9 10]\n",
      "  [11 12]]]\n",
      "\n",
      "Kernel:\n",
      "[[[ 1  0]\n",
      "  [ 0 -1]]\n",
      "\n",
      " [[ 0  1]\n",
      "  [-1  0]]]\n",
      "\n",
      "Output:\n",
      "[[[-6.  2.]\n",
      "  [-2. -2.]]\n",
      "\n",
      " [[-6.  2.]\n",
      "  [-2. -2.]]\n",
      "\n",
      " [[-3.  1.]\n",
      "  [-1. -1.]]]\n"
     ]
    }
   ],
   "source": [
    "# create an example image and kernel\n",
    "image = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]])\n",
    "kernel = np.array([[[1, 0], [0, -1]], [[0, 1], [-1, 0]]])\n",
    "\n",
    "# perform the convolution operation\n",
    "output = convolution(image, kernel)\n",
    "\n",
    "print('Input image:')\n",
    "print(image)\n",
    "\n",
    "print('\\nKernel:')\n",
    "print(kernel)\n",
    "\n",
    "print('\\nOutput:')\n",
    "print(output)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
